package com.example.leetcode.labuladong.array.prefixsum;

/**
 * https://leetcode-cn.com/problems/range-sum-query-immutable/
 *
 * 前缀和
 */
class NumArray {

    int preNums[];

    public NumArray(int[] nums) {
        preNums = new int[nums.length + 1];
        for(int i = 0; i < nums.length; i++) {
            preNums[i + 1] = preNums[i] + nums[i];
        }
    }
    
    public int sumRange(int left, int right) {
        return preNums[right + 1] - preNums[left];
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(left,right);
 */